package com.yunkaweilai.android.view.dialog.shop;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import org.greenrobot.eventbus.EventBus;

/**
 * 新增单位
 * Created by Administrator on 2016/12/22 0022.
 */

public class ShopCompanyAddDialog extends BaseDialog<ShopCompanyAddDialog> {
    ImageView idImgClose;
    TextView idTvRight;
    EditText idEdtTypeName;
    private Context context;
    public ShopCompanyAddDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_company_add, null);
        idImgClose = (ImageView) inflate.findViewById(R.id.id_img_close);
        idTvRight = (TextView) inflate.findViewById(R.id.id_tv_right);
        idEdtTypeName = (EditText) inflate.findViewById(R.id.id_edt_type_name);
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));

        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())){
                    addType();
                }
            }
        });
    }

    private void addType(){
        HttpRequest.postUrl(Api.ADD_COMPANY_SHOP)
                .addParams("unit_name",idEdtTypeName.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_COMPANY_REFRESH,true));
                            MyToast.showShortToast(context,"操作成功");
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        MyToast.showShortToast(context,"新增单位失败");
                    }
                });

    }

}
